
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<div id="dialog-confirm" title="Delete File" style="display:none;">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Are you sure?</p>
</div>

<?php
$this->breadcrumbs = array(
    'Posts' => array('index'),
    $modelPost->post_id => array('view', 'id' => $modelPost->post_id),
    'Update',
);
?>
<div class="box">
    <header>
        <h5>Update post #<?php echo $modelPost->post_id ?></h5>
    </header>
    <div class="body">	
	<?php
	$form = $this->beginWidget('CActiveForm', array(
	    'htmlOptions' => array(
		'class' => 'form-horizontal',
		'enctype' => "multipart/form-data"
	)));
	?>
	<?php if ($form->errorSummary($modelPost)) { ?>
    	<div class='alert alert-danger'>
		<?php echo $form->errorSummary($modelPost); ?>
    	</div>
	<?php } ?>
	<?php if ($success) { ?>
    	<div class='alert alert-success'>
    	    You have updated successfully.
    	</div>
	<?php } ?>
	<fieldset>

	    <legend><span></span><h2 id="portfolioHeading">Edit your post Details</h2></legend>

	    <div class="control-group">				
		<label for="inputEmail" class="control-label">Title <span>*</span></label>
		<div class="controls">
		    <?php echo $form->textField($modelPost, 'title'); ?>
		</div>
	    </div>

	    <div class="control-group">
		<label class="control-label">Content <span>*</span></label>
		<div class="controls">
		    <?php echo $form->textArea($modelPost, 'content'); ?>
		</div>
	    </div>

	    <br>
	    <br>
	    <div id="wrapper-photo-edit">
		<label>Photo</label>
		<?php
		//show attachment	
		if (isset($attachment['photo'])) {
		    foreach ($attachment['photo'] as $photo) {
			echo '<div class="wrapper-photo-edit-inner">
				<a title="Delete this Image" class="btn-delete-img delete-file" href="javascript:void(0)" post-id="' . $modelPost->post_id . '" file-name="' . $photo . '" file-type="photo">
				<i class="icon-remove"></i></a>
				<img src="' . ImageHelper::thumb(64, 64, 'photo' . DIRECTORY_SEPARATOR . $photo) . '" />
			    </div>';
		    }
		    echo '<div class="clear"></div>';
		    $inputPhoto = 5 - count($attachment['photo']);
		    if ($inputPhoto > 0) {
			if ($inputPhoto !== 5)
			    echo '<br>More photo<br>';
			for ($i = 0; $i < $inputPhoto; $i++) {
			    echo "<input type='file' name='photo[]' accept='image/*'/><br>";
			}
		    }
		}
		?>
	    </div>


	    <br>    	

	    <div id="wrapper-audio-edit">
		<label>Audio</label>	  
		<?php
		$audio = $modelPost->getAudio();
		if ($audio) {
		    echo '<div id="div-audio">' . $audio . '</div>';
		    echo '<a id="remove-audio" href="javascript:void(0)" class="delete-file"   post-id="' . $modelPost->post_id . '" file-name="' . $attachment['audio'] . '" file-type="audio"><i class="icon-remove"></i> Delete</a>';
		} else {
		    echo "<input type='file' name='audio' accept='audio/*'/><br>";
		}
		?>	    			    		


	    </div>    	
	    <br>
	    <div id="wrapper-video-edit">
		<label>Video</label>	  
		<?php
		$video = $modelPost->getVideo();
		if ($video) {
		    echo '<div id="div-video">' . $video . '</div>';
		    echo '<a id="remove-video" href="javascript:void(0)" class="delete-file"   post-id="' . $modelPost->post_id . '" file-name="' . $attachment['video']['upload'] . '" file-type="video"><i class="icon-remove"></i> Delete</a>';
		} else {
		    echo "<input type='file' name='video' accept='video/*'/><br>";
		}
		?>		
	    </div>
	    <br>
	    <div class="control-group">
		<label class="control-label">Tag <span>*</span></label>
		<div class="controls">
		    <?php echo $form->textField($modelPost, 'tags'); ?>
		</div>
	    </div>
	    <div class="control-group">
		<div class="controls">    				
		    <input type="submit" class="btn btn-large btn-info" value="Update" name='submit'/>
		</div>
	    </div>

	    <?php
	    $this->endWidget();
	    ?>


    </div>
</div>

<script>
    $(document).ready(function() {
	$("#dialog-confirm").dialog({
	    autoOpen: false,
	    show: {
		effect: "bounce",
		duration: 1000
	    },
	    hide: {
		effect: "explode",
		duration: 1000
	    }
	});

	$('.delete-file').click(function() {
	    var oBtnDelete = $(this);
	    var postId = $(this).attr('post-id');
	    var fileName = $(this).attr('file-name');
	    var fileType = $(this).attr('file-type');
	    $("#dialog-confirm").dialog({
		buttons: {
		    "Yes": function() {

			$.post('<?php echo BACKEND_SITE_URL ?>post/delete_file', {fileName: fileName, fileType: fileType, postId: postId}, function(response) {

			});
			switch (fileType.toString()) {
			    case 'photo':
				$(oBtnDelete).parent().remove();
				$('#wrapper-photo-edit').append('<input type="file" name="photo[]" accept="image/*"><br>');
				break;
			    case 'audio':
				$(oBtnDelete).parent().find('#div-audio').remove();
				$(oBtnDelete).remove();
				$('#wrapper-audio-edit').append('<input type="file" name="audio" accept="audio/*"><br>');
				break;
			    case 'video':
				$(oBtnDelete).parent().find('#div-video').remove();
				$(oBtnDelete).remove();
				$('#wrapper-video-edit').append('<input type="file" name="viddeo" accept="video/*"><br>');
				break;
			}

			$(this).dialog("close");

		    },
		    Cancel: function() {
			$(this).dialog("close");
		    }
		},
		modal: true

	    });
	    $("#dialog-confirm").dialog("open");
	});
    });
</script>